package com.qingxin.ui.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.AccordionNode;
import com.qingxin.ui.bean.TreeNode;
import com.qingxin.ui.bean.UICenterTab;
import com.qingxin.ui.bean.UICenterTabNode;
import com.qingxin.ui.bean.UILeftTabNode;
import com.qingxin.ui.bean.UIModule;
import com.qingxin.ui.bean.UIOperItem;
import com.qingxin.ui.bean.UITable;
import com.qingxin.ui.bean.UITableColumn;

public class PermissionFilter {
	
	/**
	 * 模块权限处理
	 * @param modules
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static List<UIModule> filterModules(List<UIModule> modules, UiService uiService, LoginUser loginUser){
		List<UIModule> tmpModules = new ArrayList<>();
		if(loginUser==null){//没有登录
			return tmpModules;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return modules;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights==null || rights.size()==0){
			return tmpModules;
		}
		for(UIModule module : modules){
			if(rights.contains(module.getCode().toUpperCase())){
				tmpModules.add(module);
			}
		}
		return tmpModules;
	}
	
	
	/**
	 * 菜单标签权限处理
	 * @param tabs
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static List<UILeftTabNode> filterLeftTabs(List<UILeftTabNode> tabs, UiService uiService, LoginUser loginUser){
		List<UILeftTabNode> tmpTabs = new ArrayList<>();
		if(loginUser==null){//没有登录
			return tmpTabs;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return tabs;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights == null){
			rights = new HashSet<String>();
		}
		for(UILeftTabNode tab : tabs){
			if(StringUtils.isEmpty(tab.getCode()) || rights.contains(tab.getCode())){
				tmpTabs.add(tab);
			}
		}
		return tmpTabs;
	}
	
	
	/**
	 * 处理风琴节点权限
	 * @param accordionNodes
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static List<AccordionNode> filterAccordionData(List<AccordionNode> accordionNodes, UiService uiService, LoginUser loginUser){
		List<AccordionNode> tmpNodes = new ArrayList<AccordionNode>();
		if(loginUser==null){//没有登录
			return tmpNodes;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return accordionNodes;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights == null){
			rights = new HashSet<String>();
		}
		for(AccordionNode node : accordionNodes){
			if(StringUtils.isEmpty(node.getCode()) || rights.contains(node.getCode())){
				if("tree".equalsIgnoreCase(node.getType())){
					tmpNodes.add(node.copy());
				}else{
					node = node.copy();
					filterAccordionChildrenNodes(node, rights);
					if(node.getChildren()==null || node.getChildren().size()>0){
						tmpNodes.add(node);
					}
				}
			}
		}
		return tmpNodes;
	}
	
	
	//处理风琴节点权限
	private static void filterAccordionChildrenNodes(AccordionNode node, Set<String> rights){
		List<AccordionNode> list = new ArrayList<AccordionNode>();
		List<AccordionNode> childrenList = node.getChildren();
		if(childrenList == null){
			return;
		}
		for(AccordionNode childrenNode : childrenList){
			if(StringUtils.isEmpty(childrenNode.getCode()) || rights.contains(childrenNode.getCode())){
				list.add(childrenNode);
			}
		}
		node.setChildren(list);
	}
	
	
	/**
	 * 处理树节点权限
	 * @param treeNodes
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static List<TreeNode> filterTreeData(List<TreeNode> treeNodes, UiService uiService, LoginUser loginUser){
		List<TreeNode> tmpNodes = new ArrayList<TreeNode>();
		if(loginUser==null){//没有登录
			return tmpNodes;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return treeNodes;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights == null){
			rights = new HashSet<String>();
		}
		for(TreeNode node : treeNodes){
			if(StringUtils.isEmpty(node.getCode()) || rights.contains(node.getCode())){
				node = node.copy();
				filterTreeChildrenNodes(node, rights);
				if(node.getChildren()==null || node.getChildren().size()>0){
					tmpNodes.add(node);
				}
			}
		}
		
		return tmpNodes;
	}
	
	
	//处理树节点权限
	private static void filterTreeChildrenNodes(TreeNode node, Set<String> rights){
		List<TreeNode> list = new ArrayList<TreeNode>();
		List<TreeNode> childrenList = node.getChildren();
		if(childrenList == null){
			return;
		}
		for(TreeNode childrenNode : childrenList){
			if(StringUtils.isEmpty(childrenNode.getCode()) || rights.contains(childrenNode.getCode())){
				filterTreeChildrenNodes(childrenNode, rights);
				if(childrenNode.getChildren()==null || childrenNode.getChildren().size()>0){
					list.add(childrenNode);
				}
			}
		}
		node.setChildren(list);
	}
	
	
	/**
	 * 视图标签权限处理
	 * @param centerTabNodes
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static List<UICenterTabNode> filterCenterTabNodes(List<UICenterTabNode> centerTabNodes, UiService uiService, LoginUser loginUser){
		List<UICenterTabNode> tmpNodes = new ArrayList<UICenterTabNode>();
		if(loginUser==null){//没有登录
			return tmpNodes;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return centerTabNodes;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights == null){
			rights = new HashSet<String>();
		}
		for(UICenterTabNode tabNode : centerTabNodes){
			if(StringUtils.isEmpty(tabNode.getCode()) || rights.contains(tabNode.getCode())){
				tmpNodes.add(tabNode);
			}
		}
		
		return tmpNodes;
	}
	
	
	/**
	 * 视图权限处理
	 * @param centerTab
	 * @param uiService
	 * @param loginUser
	 * @return
	 */
	public static UICenterTab filterCenterTab(UICenterTab centerTab, UiService uiService, LoginUser loginUser){
		if(loginUser == null){
			return null;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return centerTab;
		}
		Set<String> rights = uiService.getUserPermissions(loginUser.getUserid());
		if(rights == null){
			rights = new HashSet<String>();
		}
		UITable table = centerTab.getTable();
		if(table != null){
			if(table.getToolbar()!=null){
				List<UIOperItem> items = table.getToolbar().getOperItems();
				items = filterOperItems(items, rights, loginUser);
				if(items.size()==0){
					table.setToolbar(null);
				}else{
					table.getToolbar().setOperItems(items);
				}
			}
			//列配置中的操作项
			List<UITableColumn> columns = table.getOperColumns();
			for(int i=0, n=columns.size(); i<n; i++){
				UITableColumn column = columns.get(i);
				if(column.getOperItems().size() > 0){
					List<UIOperItem> items = column.getOperItems();
					items = filterOperItems(items, rights, loginUser);
					column.setOperItems(items);
				}
			}
		}
		
		return centerTab;
	}
	
	
	
	private static List<UIOperItem> filterOperItems(List<UIOperItem> operItems, Set<String> rights, LoginUser loginUser){
		List<UIOperItem> tmpItems = new ArrayList<UIOperItem>();
		if(loginUser==null){
			return tmpItems;
		}
		if(SystemCfg.isAdminUser(loginUser)){
			return operItems;
		}
		for(UIOperItem item : operItems){
			if(StringUtils.isEmpty(item.getCode()) || rights.contains(item.getCode())){
				tmpItems.add(item);
			}
		}
		return tmpItems;
	}
	

}
